package com.ocm.burnningstar.View;

import android.app.Fragment;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.blankj.utilcode.util.ScreenUtils;
import com.ocm.burnningstar.Adapter.VerUserOverAdapter;
import com.ocm.burnningstar.Interface;
import com.ocm.burnningstar.MainActivity;
import com.ocm.burnningstar.Model.UserOver;
import com.ocm.burnningstar.R;
import com.shape.sdk.FitnessData;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


@ContentView(R.layout.activity_over)
public class OverFragment extends Fragment {

    @ViewInject(R.id.list_over)
    RecyclerView list_over;
    @ViewInject(R.id.line)
    LinearLayout line;
    @ViewInject(R.id.backvideo)
    VideoView videoView1;


    int COLUMN = 4;
    boolean isFirst = true;
    VerUserOverAdapter over;
    MainActivity mainActivity;

    Interface.OverListener overLs= new Interface.OverListener() {
        @Override
        public void run(List<FitnessData> data) {
//            updateData(data);
        }
    };

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }
    @Override
    public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);
        mainActivity = (MainActivity)getActivity();
        mainActivity.SetOverListener(overLs);
        init();
    }

    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {// 不在最前端界面显示
        } else {// 重新显示到最前端中
            if(isFirst){
                init();
                isFirst = false;
            }
        }
    }

    private void init(){
//        List<UserOver> users = new ArrayList<>();
//        for(int i =0;i<3;i++){
//            final UserOver user = new UserOver();
//            user.setHeadrUrl("");
//            user.setHeartAvg(123);
//            user.setCalorie(123);
//            user.setName("12312312");
//            users.add(user);
//        }

        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), COLUMN);
        list_over.setLayoutManager(layoutManager);
        list_over.addItemDecoration(new GridSpacingItemDecoration(4,30, true));
        list_over.setHasFixedSize(true);


//        over = new VerUserOverAdapter(getActivity(),users);
//        list_over.setAdapter(over);
//        //水平排列
//        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), COLUMN);
//        list_over.setLayoutManager(layoutManager);
//        list_over.setHasFixedSize(true);
//        List<UserOver> users = new ArrayList<>();
//        over = new VerUserOverAdapter(getActivity(),users);
//        list_over.setAdapter(over);

        updateData(MainActivity.overFitnessDatas);

        String video = "android.resource://" + mainActivity.getPackageName() + "/" + R.raw.shapemp4;
        videoView1.setOnPreparedListener(new MyPrepatedListener());
        videoView1.setOnCompletionListener(new MyCompetionListener(videoView1));
        videoView1.setVideoURI(Uri.parse(video));
    }

    void setView(int len){
        int screenWidth = ScreenUtils.getScreenWidth();
        int screenHeight = (int)(ScreenUtils.getScreenHeight());
        FrameLayout.LayoutParams linearLayoutParams = (FrameLayout.LayoutParams) line.getLayoutParams();
        int margintop =(int) (screenHeight*0.15);
        linearLayoutParams.width =(int)(screenWidth*0.78);
        linearLayoutParams.height =(int)(screenHeight*0.7);
        int outLeft = (int)(screenWidth*0.11);
        int marginleft =outLeft+linearLayoutParams.width*(4-len)/8;
        linearLayoutParams.setMargins(marginleft,margintop,0,0);
        line.setLayoutParams(linearLayoutParams);
    }

    public void updateData(List<FitnessData> data){
        final List<UserOver> users = new ArrayList<>();
        if(data==null)return;

        int len = data.size()>=4?4:data.size();
        if(len>0){
//            videoView1.stopPlayback();
            videoView1.setVisibility(View.INVISIBLE);
        }else{
            videoView1.setVisibility(View.VISIBLE);
        }
        setView(len);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), COLUMN);
        list_over.setLayoutManager(layoutManager);
        list_over.setHasFixedSize(true);
        list_over.addItemDecoration(new GridSpacingItemDecoration(len,30, true));
        for(int i =0;i<len;i++){
            final UserOver user = new UserOver();
            FitnessData fd = data.get(i);
            user.setHeadrUrl(fd.getUser().getAvatar());
            user.setHeartAvg(fd.getHeartRate());
            user.setCalorie(fd.getCalories());
            user.setName(fd.getUser().getName());
            users.add(user);
        }
        Log.i("updateData","user size:"+users.size());
        over = new VerUserOverAdapter(getActivity(),users);
        list_over.setAdapter(over);
    }

    public class MyPrepatedListener implements MediaPlayer.OnPreparedListener {
        @Override
        public void onPrepared(MediaPlayer mediaPlayer) {
            videoView1.start();
        }
    }

    public class MyCompetionListener implements MediaPlayer.OnCompletionListener {
        final VideoView videoView;
        MyCompetionListener(VideoView videoView) {
            this.videoView = videoView;
        }

        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            videoView.start();
        }
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        Log.d("over","onConfigurationChanged");
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            double mwidth = layoutParams.height*0.5625;
            layoutParams.width = layoutParams.width>mwidth?(int)mwidth:layoutParams.width;
            videoView1.setLayoutParams(layoutParams);
        }
        super.onConfigurationChanged(newConfig);
    }
}
